Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | 9x 9x 9x 9x 9x 8x 8x 9x 9x 8x 36x 8x 8x 8x 24x 14x 14x 8x 8x 8x 12x 8x 9x 30x 30x 12x 9x 12x 12x 12x 9x 9x 9x 9x 4x 4x 4x 8x 8x | import { getSession } from "@/lib/auth";
import { redirect } from "next/navigation";
import Link from "next/link";
import type { Item } from "@/types";
import { getDb } from "@/lib/db";
import { requireCollectionAccess } from "@/lib/collection-access";
import { ItemController } from "@/server/api/items";
import { DispositionController } from "@/server/api/dispositions";
import { DecisionController } from "@/server/api/decisions";
import { PhotoController } from "@/server/api/photos";
import { PreferenceController } from "@/server/api/preferences";
import { RankingController } from "@/server/api/ranking";
import { SessionController } from "@/server/api/sessions";
import { buildRanking } from "@/server/services/sortResultMaterialization";
import { getPhotoAssetUrlForPhoto, PHOTO_WIDTH } from "@/lib/photos";
import { deriveUsage, type UsageAnswer } from "@/lib/derive-usage";
import { itemDisplayName, itemInitial } from "@/lib/items";
import { quietLink } from "@/lib/design/quiet-link";
import { AppLayout } from "@/components/layout";
import { trail } from "@/components/layout/breadcrumb-trails";
import { CardImage, buttonVariants } from "@/components/ui";
import { WorkspaceModeChips } from "@/features/collections/WorkspaceModeChips";
import { PendingDecisionsGate } from "@/features/sorting/PendingDecisionsGate";
import { PendingNotices } from "@/features/sorting/pendingNotices";
import { BringBackControl } from "@/features/sorting/BringBackControl";
import { ItemCard } from "@/features/items/ItemCard";
import { MostLovedHero } from "@/features/results/MostLovedHero";
import { RankingList, type RankingRow } from "@/features/results/RankingList";
import { SaveAndShare } from "@/features/results/SaveAndShare";
import { BUCKET_LABELS } from "@/features/sorting/roundCopy";
interface RankingPageProps {
params: Promise<{ id: string }>;
}
/**
* The live Ranking (spec §4.3). Reads only — viewing creates nothing (regression:
* ranking-view-creates-nothing.integration.test.ts). Hand moves pin into the one
* open draft through the pins API; Save and share freezes it.
*/
export default async function RankingPage({ params }: RankingPageProps) {
const { id } = await params;
const session = await getSession();
Iif (!session?.user) {
redirect("/login");
}
const db = getDb();
const collection = await requireCollectionAccess(db, id, session.user.id);
const curatorId = session.user.id;
const slug = collection.slug ?? collection.id;
const base = `/collections/${slug}`;
const [preferences, dispositions, items, curatorEvents, pins, sessions] = await Promise.all([
PreferenceController.fromDb(db).listItemPreferences(collection.id, curatorId),
DispositionController.fromDb(db).listDispositions(collection.id, curatorId),
ItemController.fromDb(db).listItems(collection.id),
// Server-side only: folded into a small usage map, never sent to the client (DATA-6).
DecisionController.fromDb(db).listForCurator(collection.id, curatorId),
RankingController.fromDb(db).listPins(collection.id, curatorId),
SessionController.fromDb(db).listByCollectionAndCurator(collection.id, curatorId),
]);
const sortOpen = sessions.some((s) => s.status === "active" || s.status === "paused");
const itemById = new Map(items.map((i) => [i.id, i]));
const liveIds = new Set(itemById.keys());
const { loved, onward } = buildRanking(preferences, liveIds, pins);
const usageByItemId = deriveUsage(curatorEvents, liveIds);
const lookup = (ids: string[]) => ids.flatMap((itemId) => {
const item = itemById.get(itemId);
return item ? [item] : [];
});
const stillDeciding = lookup(dispositions.filter((d) => d.disposition === "deferred").map((d) => d.itemId));
const setAside = lookup(dispositions.filter((d) => d.disposition === "set_aside").map((d) => d.itemId));
const onwardItems = lookup(onward.map((e) => e.itemId));
const shownIds = [...loved.map((e) => e.itemId), ...onwardItems.map((i) => i.id), ...stillDeciding.map((i) => i.id), ...setAside.map((i) => i.id)];
const covers = shownIds.length > 0 ? await PhotoController.fromDb(db).findCoverPhotosByItemIds(shownIds) : new Map();
const coverUrl = (itemId: string) => {
const cover = covers.get(itemId);
return cover ? getPhotoAssetUrlForPhoto(cover, collection.id, { w: PHOTO_WIDTH.card }) : undefined;
};
const hrefOf = (item: Item) => (item.number === null ? base : `${base}/items/${item.number}`);
const rows: RankingRow[] = loved.flatMap((entry) => {
const item = itemById.get(entry.itemId);
Iif (!item) return [];
return [{
itemId: item.id,
href: hrefOf(item),
name: itemDisplayName(item),
coverUrl: coverUrl(item.id),
initial: itemInitial(item),
tier: BUCKET_LABELS[entry.bucket],
pinned: entry.pinned,
usage: usageByItemId.get(item.id),
}];
});
const hero = rows.length > 0 ? itemById.get(rows[0].itemId) : undefined;
const hasRanking = rows.length > 0 || onwardItems.length > 0;
const defaultName = `${collection.name}, ${new Date().toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric" })}`;
return (
<AppLayout breadcrumbs={trail.results(collection)}>
{/* Queued answers land before the ranking is read. */}
<PendingDecisionsGate curatorId={curatorId} collectionId={collection.id}>
<div className="space-y-8">
<PendingNotices collectionId={collection.id} />
<header className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
<div>
<h1 id="ranking-heading" tabIndex={-1} className="font-display text-2xl text-ink focus:outline-hidden sm:text-3xl">
Your ranking
</h1>
<p className="mt-2 text-ink-soft">Move any piece by hand and it stays where you put it.</p>
</div>
{hasRanking && (
<div className="flex flex-col items-stretch gap-2 sm:items-end">
<SaveAndShare collectionId={collection.id} collectionSlug={slug} curatorId={curatorId} defaultName={defaultName} />
<Link href={`${base}/sort`} className={quietLink}>
Keep sorting
</Link>
</div>
)}
</header>
<WorkspaceModeChips base={base} activeMode="decide" />
{hasRanking ? (
<>
{hero && <MostLovedHero item={hero} collectionId={slug} heroPhotoUrl={coverUrl(hero.id)} />}
{rows.length > 0 && <RankingList collectionId={collection.id} rows={rows} />}
{onwardItems.length > 0 && (
<ItemRow id="ranking-onward" heading="Onward" caption="Where these go next." items={onwardItems} slug={slug} coverUrl={coverUrl} usageOf={(itemId) => usageByItemId.get(itemId)} />
)}
</>
) : (
<section className="space-y-3">
<h2 className="font-display text-2xl text-ink">Nothing to see yet — and that’s fine.</h2>
<p className="text-ink-soft">Once you’ve sat with a few, what you love most will settle here.</p>
<Link href={`${base}/sort`} className={buttonVariants()}>
{sortOpen ? "Keep sorting" : "Start sorting"}
</Link>
</section>
)}
{stillDeciding.length > 0 && (
<ItemRow id="ranking-still-deciding" heading="Still deciding" caption="Ready when you are." items={stillDeciding} slug={slug} coverUrl={coverUrl} usageOf={(itemId) => usageByItemId.get(itemId)} />
)}
{setAside.length > 0 && (
<details className="group rounded-card border border-rim bg-glass p-4">
<summary className="flex min-h-11 cursor-pointer list-none items-center gap-3 rounded-control text-ink focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-primary [&::-webkit-details-marker]:hidden">
<span className="flex-1">Set aside</span>
<svg aria-hidden viewBox="0 0 24 24" fill="none" className="h-5 w-5 shrink-0 text-ink-soft transition-transform group-open:rotate-180 motion-reduce:transition-none">
<path d="M6 9l6 6 6-6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</summary>
<ul className="mt-3 space-y-3">
{setAside.map((item) => (
<li key={item.id} className="flex items-center gap-3">
<span className="relative flex h-12 w-12 shrink-0 items-center justify-center overflow-hidden rounded-control bg-content-soft">
<CardImage src={coverUrl(item.id)} alt="" fallback={<span aria-hidden className="text-ink-soft">☾</span>} />
</span>
<span className="min-w-0 flex-1 truncate text-ink">{itemDisplayName(item)}</span>
<BringBackControl collectionId={collection.id} itemId={item.id} itemName={itemDisplayName(item)} focusAfterLastId="ranking-heading" />
</li>
))}
</ul>
</details>
)}
</div>
</PendingDecisionsGate>
</AppLayout>
);
}
function ItemRow({
id,
heading,
caption,
items,
slug,
coverUrl,
usageOf,
}: {
id: string;
heading: string;
caption: string;
items: Item[];
slug: string;
coverUrl: (itemId: string) => string | undefined;
usageOf: (itemId: string) => UsageAnswer | undefined;
}) {
return (
<section aria-labelledby={id} className="space-y-3">
<div>
<h2 id={id} className="font-display text-xl text-ink">
{heading}
</h2>
<p className="text-sm text-ink-soft">{caption}</p>
</div>
<ul className="grid grid-cols-2 gap-3 sm:grid-cols-3 lg:grid-cols-4">
{items.map((item) => (
<li key={item.id}>
<ItemCard item={item} collectionId={slug} heroPhotoUrl={coverUrl(item.id)} usage={usageOf(item.id)} />
</li>
))}
</ul>
</section>
);
}
|